﻿
// ChildView.cpp: CChildView 类的实现
//

#include "stdafx.h"
#include "Hanoi.h"
#include "ChildView.h"
#include "CHanoi.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CChildView

CChildView::CChildView()
{
}

CChildView::~CChildView()
{
}


BEGIN_MESSAGE_MAP(CChildView, CWnd)
	ON_WM_PAINT()
	ON_COMMAND(ID_TEST3, &CChildView::OnTest3)
	ON_COMMAND(ID_TEST4, &CChildView::OnTest4)
	ON_COMMAND(ID_TEST8, &CChildView::OnTest8)
END_MESSAGE_MAP()



// CChildView 消息处理程序

BOOL CChildView::PreCreateWindow(CREATESTRUCT& cs) 
{
	if (!CWnd::PreCreateWindow(cs))
		return FALSE;

	cs.dwExStyle |= WS_EX_CLIENTEDGE;
	cs.style &= ~WS_BORDER;
	cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS, 
		::LoadCursor(nullptr, IDC_ARROW), reinterpret_cast<HBRUSH>(COLOR_WINDOW+1), nullptr);

	return TRUE;
}

CDC *myDC;
CHanoi hn;
int HanoiNum = 0;
int step = 0;
void gdiDrawPlate(int id, CRect rect)
{
	CString str;
	myDC->Rectangle(&rect);
	str.Format(L"%d", id);

	CPoint p = rect.CenterPoint();
	myDC->TextOutW(p.x - 5, p.y - PLATE_HEIGHT / 2 + 3, str);
}

void gdiDrawLine(int x0, int y0, int x1, int y1)
{
	myDC->MoveTo(x0, y0);
	myDC->LineTo(x1, y1);
}

void ShowPole(int sleeptime)
{
	CBrush brush;//申明一个格式刷对象
	brush.CreateSolidBrush(RGB(255, 255, 255));//设置颜色
	myDC->FillRect(CRect(0, 0, 500, 400), &brush);

	hn.Draw();

	CString str;
	str.Format(L"steps = %d", hn.GetSteps());
	myDC->DrawText(str, CRect(50, 320, 550, 350), DT_LEFT | DT_VCENTER);
	if (sleeptime)
		Sleep(sleeptime);
}

void CChildView::OnPaint() 
{
	CPaintDC dc(this); // 用于绘制的设备上下文
	myDC = &dc;

	if (hn.IsEmpty())
	{
		ShowPole(0);
		return;
	}
	ShowPole(PAUSE_TIME);
	hn.move(HanoiNum, 0, 2, 1);
}

void CChildView::OnTest3()
{
	// TODO: 在此添加命令处理程序代码
	HanoiNum = 3;
	step = 0;
	hn.Init(HanoiNum);
	Invalidate();
}

void CChildView::OnTest4()
{
	HanoiNum = 4;
	step = 0;
	hn.Init(HanoiNum);
	Invalidate();
}

void CChildView::OnTest8()
{
	HanoiNum = 8;
	step = 0;
	hn.Init(HanoiNum);
	Invalidate();
}
